package com.atguigu.spzx.product.controller;

import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.product.ProductItemVo;
import com.atguigu.spzx.model.vo.product.ProductSkuDto;
import com.atguigu.spzx.product.service.ProductService;
import com.atguigu.spzx.product.service.ProductSkuService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductSkuService productSkuService;


    @GetMapping("/{page}/{limit}")
    public Result<PageInfo<ProductSku>> findByPage(@PathVariable Integer page, @PathVariable Integer limit,
                                                   ProductSkuDto productSkuDto){
        System.out.println(productSkuDto);
        PageInfo<ProductSku> pageInfo= productService.findByPage(page,limit,productSkuDto);
        return Result.ok(pageInfo);
    }

    @GetMapping("/item/{skuId}")
    public Result<ProductItemVo> item(@PathVariable Long skuId){
        ProductItemVo productItemVo= productService.item(skuId);
        return Result.ok(productItemVo);
    }

    //这是购物车内部调用接口,不需要result封装.
    @GetMapping("/getBySkuId/{skuId}")
    public ProductSku getSkuById(@PathVariable Long skuId){
        ProductSku productSku= productSkuService.getSkuById(skuId);
        return productSku;
    }

    @PutMapping("updateStock/{skuId}/{skuNum}")
    public Boolean updateStock(@PathVariable Long skuId,@PathVariable Integer skuNum){
        Boolean success = productService.updateStock(skuId,skuNum);
        return success;
    }
}
